package com.fh.service.impl;
import com.fh.feign.OrderItemQueryClient;
import com.fh.feign.StockUpdateClient;
import com.fh.feign.StockqueryClient;
import com.fh.mapper.OrderCloseMapper;
import com.fh.model.OrderItem;
import com.fh.model.Orders;
import com.fh.model.ProductSku;
import com.fh.service.OrderCloseService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderCloseServiceImpl  implements OrderCloseService {
    @Resource
    private OrderCloseMapper orderCloseMapper;
    @Resource
    private OrderItemQueryClient orderItemQueryClient;
    @Resource
    private StockqueryClient stockqueryClient;
   @Resource
   private StockUpdateClient stockUpdateClient;

    @Override
    public int close(String orderId) {
        Orders orders1=new Orders();
        orders1.setOrderId(orderId);
        orders1.setStatus("6");
        orders1.setCloseType(1);
        int i = orderCloseMapper.updateByPrimaryKeySelective(orders1);
        if (i>0){
            //还原库存
            List<Integer> list =new ArrayList<>();
            List<OrderItem> orderItems = orderItemQueryClient.query(orderId);
            for (OrderItem orderItem : orderItems) {
                ProductSku productSku = stockqueryClient.queryStock(orderItem.getSkuId());
                productSku.setStock(productSku.getStock()+orderItem.getBuyCounts());
                stockUpdateClient.update(productSku);
            }
        }
        return i;
    }
}
